﻿using DevExpress.XtraBars;
using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.Nodes;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;

namespace FromUtils
{
   public static class TreeListTool
    {
       /// <summary>
       /// 获取有焦点的节点
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="p"></param>
       /// <returns></returns>
       public static TreeListNode GetFocusNode(Object sender, Point p)
       {
           TreeList currentTreeList = sender as TreeList;
           TreeListHitInfo hitinfo = currentTreeList.CalcHitInfo(p);
           if (hitinfo.Node != null)
           {
               return hitinfo.Node;
           }
           return null;
       }

       /// <summary>
       /// 遍历指定节点下的所有节点
       /// </summary>
       /// <param name="tns"></param>
       /// <returns></returns>
       public static List<TreeListNode> GetTreelistAllNode(TreeListNode tns)
       {

           List<TreeListNode> LTLN = new List<TreeListNode>();
           List<System.Collections.IEnumerator> NodeIEnumerator = new List<System.Collections.IEnumerator>();
           NodeIEnumerator.Add(tns.Nodes.GetEnumerator());
       NodeIEnumeratorIndex:
           //重新赋值节点，用户记录当前层级节点
           List<System.Collections.IEnumerator> NodeIEnumeratoritme = new List<System.Collections.IEnumerator>();
           foreach (System.Collections.IEnumerator ListCopy in NodeIEnumerator)
           {
               NodeIEnumeratoritme.Add(ListCopy);
           }
           NodeIEnumerator.Clear();
           //获取节点迭代器
           foreach (System.Collections.IEnumerator en in NodeIEnumeratoritme)
           {
               //遍历本层级节点
               while (en.MoveNext())
               {
                   //获取转换当前节点数据类型
                   TreeListNode node = (TreeListNode)en.Current;

                   LTLN.Add(node);

                   NodeIEnumerator.Add(node.Nodes.GetEnumerator());
               }
           }
           if (NodeIEnumerator.Count > 0)
           {
               goto NodeIEnumeratorIndex;
           }
           return LTLN;
       }

       /// <summary>
       /// 自定义变量，以点方式组合${XXX.XXX}
       /// </summary>
       public static void GetDotVarList(TreeList tl)
       {
           List<string> NodeRootString = new List<string>();
           List<System.Collections.IEnumerator> NodeIEnumerator = new List<System.Collections.IEnumerator>();
           NodeIEnumerator.Add(tl.Nodes.GetEnumerator());
           NodeRootString.Clear();
       NodeIEnumeratorIndex:
           //重新赋值节点，用户记录当前层级节点
           List<System.Collections.IEnumerator> NodeIEnumeratoritme = new List<System.Collections.IEnumerator>();
           foreach (System.Collections.IEnumerator ListCopy in NodeIEnumerator)
           {
               NodeIEnumeratoritme.Add(ListCopy);
           }
           NodeIEnumerator.Clear();
           //获取节点迭代器
           foreach (System.Collections.IEnumerator en in NodeIEnumeratoritme)
           {
               //遍历本层级节点
               while (en.MoveNext())
               {
                   //获取转换当前节点数据类型
                   DevExpress.XtraTreeList.Nodes.TreeListNode node = (DevExpress.XtraTreeList.Nodes.TreeListNode)en.Current;
                   if (node.ParentNode == null)
                   {
                       node.SetValue("Parameter", node.GetValue("Key").ToString());
                       NodeRootString.Add(node.GetValue("Key").ToString());
                   }
                   else
                   {
                       DevExpress.XtraTreeList.Nodes.TreeListNode ParentNode = null;
                       ParentNode = node;
                       string ParentNodetext = "";
                   ParentNodeIndex:
                       ParentNode = ParentNode.ParentNode;
                       if (ParentNode != null)
                       {
                           ParentNodetext = ParentNode.GetValue("Key").ToString() + "." + ParentNodetext;
                           goto ParentNodeIndex;
                       }
                       ParentNodetext = ParentNodetext + node.GetValue("Key").ToString();
                       node.SetValue("Parameter", ParentNodetext);
                       NodeRootString.Add(ParentNodetext);
                   }
                   NodeIEnumerator.Add(node.Nodes.GetEnumerator());
               }
           }
           if (NodeIEnumerator.Count > 0)
           {
               goto NodeIEnumeratorIndex;
           }
       }
    
   }
}
